package com.woniu.service.impl;


import com.woniu.entity.FindStudentUtile;
import com.woniu.entity.Score1;
import com.woniu.entity.Student1;
import com.woniu.entity.Teachercourse1;
import com.woniu.mapper.*;
import com.woniu.pojo.*;
import com.woniu.pojo.Class;
import com.woniu.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.jnlp.IntegrationService;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ScoreServiceImpl implements ScoreService {

    @Autowired
    ScoreDao scoreDao;
    @Autowired
    ClassDao classDao;
    @Autowired
    DeptDao deptDao;
    @Autowired
    MajorDao majorDao;
    @Autowired
    StudentService studentService;
    @Autowired
    TeacherCourseService teacherCourseService;
    @Autowired
    CourseDao courseDao;

    /**
     * 增加学生信息
     * @param score
     */
    @Override
    public void add(Score score) {
        scoreDao.insert(score);
    }

    /**
     * 修改成绩信息
     * @param score
     */
    @Override
    public void updateById(Score score) {
        Score1 score1 = scoreDao.selectByPrimaryKey1(score.getScoreId());
        Student student=score1.getStudentId();
        if (score.getScoreNum()>=60) {
            //如果以前成绩为空或者不及格
            if (score1.getScoreNum()==null||score1.getScoreNum()<60) {
                //这次成绩及格
    //           得到课程的学分
                Integer courseScore = courseDao.selectByPrimaryKey(score.getCourseId()).getCourseScore();
    //           修改学生学分
                float newGrade=0;
                if (student.getStudentGrade()==null) {
                    newGrade=0+courseScore;
                }else{
                    newGrade=student.getStudentGrade()+courseScore;
                }

                student.setStudentGrade(newGrade);
                studentService.updateById(student);
    //           修改成绩
                scoreDao.updateByPrimaryKeySelective(score);
            }else{
//                以前成绩及格，只修改成绩不增加学分
                scoreDao.updateByPrimaryKeySelective(score);
            }
        }else{
            //考试成绩不及格只修改成绩，不增加学分
            scoreDao.updateByPrimaryKeySelective(score);
        }
    }

    /**
     * 学生查询自己所有科目成绩
     * */
    @Override
    public List<Score1> findStuScore(Integer studentId) {
        ScoreExample scoreExample=new ScoreExample();
        scoreExample.createCriteria().andStudentIdEqualTo(studentId);
        List<Score1> scores = scoreDao.selectByExample1(scoreExample);
        return scores;
    }

    /**
     * 讲师
     * 查询自己所带课程的学生成绩
     * 可以根据学生姓名模糊查询学生成绩
     * 可以根据班级查询自己所带课程的学生成绩
     * */
    @Override
    public List<Score1> findJsScore(Integer empno, FindStudentUtile studentUtile) {
        ScoreExample scoreExample=new ScoreExample();
        scoreExample.setOrderByClause("course_id,score_num desc");
        ScoreExample.Criteria criteria = scoreExample.createCriteria();
        //添加讲师查询条件
        criteria.andEmpIdEqualTo(empno);
        isExample(studentUtile, criteria);
        List<Score1> scores = scoreDao.selectByExample1(scoreExample);
        System.out.println(scores.size());
        for (Score1 e : scores) {
            System.out.println(e.getStudentId());
        };
        return scores;
    }
    /**
    * 班主任
    * 查询所带班级的学生成绩
    * 可以根据学生姓名模糊查询
    * 根据科目查询
    * */
    @Override
    public List<Score1> findBzrScore(Integer empno, FindStudentUtile studentUtile) {
//        查询到班主任所带的班级
        ClassExample classExample=new ClassExample();
        classExample.createCriteria().andEmpIdEqualTo(empno);
        List<Class> classes = classDao.selectByExample(classExample);
        List<Integer> calssIds=new ArrayList<>();
        for (Class e : classes) {
            calssIds.add(e.getClassId());
        }
//        通过班主任所带的班级查询所有学生成绩
        ScoreExample scoreExample=new ScoreExample();
        scoreExample.setOrderByClause("course_id,score_num desc");
        ScoreExample.Criteria criteria = scoreExample.createCriteria();
        criteria.andClassIdIn(calssIds);
        isExample(studentUtile, criteria);
        List<Score1> scores = scoreDao.selectByExample1(scoreExample);
        return scores;
    }

    /**
     * 就业专员
     * 可以查看所有学员成绩
     * 可以根据院系查询
     * 可以根据姓名模糊查询
     * 可以根据科目查询
     * 可以根据专业查询
     * 可以根据班级查询
     * */
    @Override
    public List<Score1> findJyzyScore(FindStudentUtile studentUtile) {
//        就业专员查询所有学生成绩
        ScoreExample scoreExample=new ScoreExample();
        scoreExample.setOrderByClause("course_id,score_num desc");
        ScoreExample.Criteria criteria = scoreExample.createCriteria();
        isExample(studentUtile, criteria);
        List<Score1> scores = scoreDao.selectByExample1(scoreExample);
        return scores;
    }

    @Override
    public List<Score1> findByExample(Integer stuId, Integer courseId) {
        ScoreExample scoreExample=new ScoreExample();
        scoreExample.createCriteria().andStudentIdEqualTo(stuId).andCourseIdEqualTo(courseId);
        List<Score1> scores = scoreDao.selectByExample1(scoreExample);

        return scores;
    }

    /**
     * 判断查询条件是否为空，并给查询条件赋值
     * */
    @Override
    public void isExample(FindStudentUtile studentUtile,ScoreExample.Criteria criteria) {
//        判断根据学生名搜索
        if (studentUtile.getStuName()!=null&&!studentUtile.getStuName().equals("")) {
            Student student=new Student();
            student.setStudentName(studentUtile.getStuName());
            List<Student1> all = studentService.findAll(student);
            List<Integer> list=new ArrayList<>();
            if (all.size()!=0) {
                for (Student1 e : all) {
                    list.add(e.getStudentId());
                }
            }else{
                list.add(-1);
            }
            //            将学生信息输入查询条件
            criteria.andStudentIdIn(list);
        }
//        判断根据专业搜索是否为空
        if (studentUtile.getMajorId()!=null) {
//            通过专业查询班级
            ClassExample classExample=new ClassExample();
            classExample.createCriteria().andMajorIdEqualTo(studentUtile.getMajorId());
            List<Class> classes = classDao.selectByExample(classExample);
            List<Integer> list=new ArrayList<>();
            if(classes.size()!=0) {
                for (Class e : classes) {
                    list.add(e.getClassId());
                }
            }else{
                list.add(-1);
            }
            //            将班级信息存入查询条件
            criteria.andClassIdIn(list);
        }
//        判断根据院系搜索
        if (studentUtile.getDeptId()!=null) {
            ClassExample classExample=new ClassExample();
            classExample.createCriteria().andDeptIdEqualTo(studentUtile.getDeptId());
            List<Class> classes = classDao.selectByExample(classExample);
            List<Integer> calssIds=new ArrayList<>();

            if (classes.size()>0) {
                for (Class e : classes) {
                    calssIds.add(e.getClassId());
                }
            }else{
                calssIds.add(-1);
            }
//        将班级信息输入查询条件
            criteria.andClassIdIn(calssIds)	;
        }
//        判断根据班级搜索
        if (studentUtile.getClassId()!=null) {
            criteria.andClassIdEqualTo(studentUtile.getClassId());
        }
//        判断根据课程搜索
        if (studentUtile.getCourseId()!=null) {
//            将班级信息存入查询条件
            criteria.andCourseIdEqualTo(studentUtile.getCourseId());
        }
    }

    @Override
    public List<Score1> findByClassAndCourse(Integer classId, Integer courseId) {
        ScoreExample scoreExample=new ScoreExample();
        scoreExample.createCriteria().andClassIdEqualTo(classId).andCourseIdEqualTo(courseId);
        List<Score1> scores = scoreDao.selectByExample1(scoreExample);
        return scores;
    }

    @Override
    public List<Score1> findByEmpId(Integer empId) {
        ScoreExample example=new ScoreExample();
        example.createCriteria().andEmpIdEqualTo(empId);
        List<Score1> list = scoreDao.selectByExample1(example);
        return list;
    }

}
